﻿using Kele.Activity.Helper;
using Kele.Repository;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;

namespace Kele.Activity.Models
{
    [Table("kl_activity")]
    public class ActivityEntity
    {
        public int Id { get; set; }
        public int FormId { get; set; }
        public int MerchantId { get; set; }
        public int SkinId { get; set; }
        public int MusicId { get; set; }
        public string Title { get; set; }
        public ActivityStatus Status { get; set; }
        public int BuyerNum { get; set; }
        public int BrowseNum { get; set; }
        public int SoldNum { get; set; }
        public int ShareNum { get; set; }
        public bool IsRecommend { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public string Rule { get; set; }
        public string Description { get; set; }

        public int RemainNum =>GetProperty<int>("goodNum") - SoldNum;
        public int LimitPerPerson => GetProperty<int>("limitPerPerson");
        public decimal? Price => GetProperty<decimal>("price");

        public decimal? PrePrice =>  GetProperty<decimal>("prePrices");

        public List<string> RangeDate => new List<string> { StartTime.ToString(CultureInfo.InvariantCulture), EndTime.ToString(CultureInfo.InvariantCulture) };

        [DefaultValue("[]")]
        [JsonConverter(typeof(StringJsonConverter))]
        public string Fields { get; set; }

        [DefaultValue("[]")]
        [JsonConverter(typeof(StringJsonConverter))]
        public string Images { get; set; }


        [DefaultValue("{}")]
        [JsonConverter(typeof(StringJsonConverter))]
        public string Props { get; set; }

        [DefaultValue("[]")]
        [JsonConverter(typeof(StringJsonConverter))]
        public string TopImages { get; set; }

        [DefaultValue("[]")]
        [JsonConverter(typeof(StringJsonConverter))]
        public string Stores { get; set; }


        private JObject _props;

        private T GetProperty<T>(string propertyName)
        {

            if (_props == null) _props = this.Props.ToJObject();
            var token = _props.GetValue(propertyName);
            if (token== null) return default;
            if (!token.HasValues) return default;
            return token.Value<T>(); 
        }
    }


    /// <summary>
    /// 活动状态，1=上线、0=下线
    /// </summary>
    public enum ActivityStatus
    {

        Online = 1,
        Offline = 0,
    }


    public class ActivityField
    {

        public string Lable { get; set; }
        public string Type { get; set; }
        public dynamic Options { get; set; }

    }
}